package com.woohua.demo.service.feature;

import com.woohua.demo.entity.UserInfo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/***
 * @title AndTaskService
 * @description
 * @author woo hua
 * @version 1.0.0
 * @create 2023/2/24 8:54
 **/
@Service
public class AndTaskService {

    public static void andTask() throws ExecutionException, InterruptedException {
        thenCombine();
    }


    private static void thenCombine() throws ExecutionException, InterruptedException {
        // 10. thenCombine 合并结果
        CompletableFuture<List<UserInfo>> future10 = CompletableFuture.supplyAsync(() -> {
            sleep(100);
            List<UserInfo> list = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                System.out.println("=====" + i);
                UserInfo userInfo = new UserInfo();
                userInfo.setUsername("张三" + i);
                userInfo.setPassword(String.valueOf(i));
                userInfo.setEnabled("true");
                list.add(userInfo);
            }
            return list;
        }).thenCombine(CompletableFuture.supplyAsync(() -> {
            sleep(100);
            List<UserInfo> list = new ArrayList<>();
            for (int i = 10; i < 20; i++) {
                System.out.println("=====" + i);
                UserInfo userInfo = new UserInfo();
                userInfo.setUsername("张三" + i);
                userInfo.setPassword(String.valueOf(i));
                userInfo.setEnabled("true");
                list.add(userInfo);
            }
            return list;
        }), (taskAReturnVal, taskBReturnVal) -> {
            taskAReturnVal.addAll(taskBReturnVal);
            return taskAReturnVal;
        }).thenCombine(CompletableFuture.supplyAsync(() -> {
            sleep(100);
            List<UserInfo> list = new ArrayList<>();
            for (int i = 20; i < 30; i++) {
                System.out.println("=====" + i);
                UserInfo userInfo = new UserInfo();
                userInfo.setUsername("张三" + i);
                userInfo.setPassword(String.valueOf(i));
                userInfo.setEnabled("true");
                list.add(userInfo);
            }
            return list;
        }), (taskAReturnVal, taskBReturnVal) -> {
            taskAReturnVal.addAll(taskBReturnVal);
            return taskAReturnVal;
        }).thenCombine(CompletableFuture.supplyAsync(() -> {
            sleep(100);
            List<UserInfo> list = new ArrayList<>();
//            for (int i = 20; i < 30; i++) {
//                System.out.println("=====" + i);
//                UserInfo userInfo = new UserInfo();
//                userInfo.setUsername("张三" + i);
//                userInfo.setPassword(String.valueOf(i));
//                userInfo.setEnabled("true");
//                list.add(userInfo);
//            }
            return list;
        }), (taskAReturnVal, taskBReturnVal) -> {
            taskAReturnVal.addAll(taskBReturnVal);
            return taskAReturnVal;
        });
        System.out.println("10.3 get: " + future10.get());
        sleep(200);

    }

    private static void sleep(long millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException ie) {
            Thread.currentThread().interrupt();
        }
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        andTask();
        List<Integer> price = section(10001, 2000);
        System.out.println("========元组：" + price);
    }

    /**
     * 将 max 按 limit 分段
     *
     * @param max
     * @param limit 分组
     * @return
     */
    public static List<Integer> section(int max, int limit) {

        ArrayList<Integer> arrayList = new ArrayList<>();

        //如果最大值小于10 直接没有分段...
        if (max <= limit) {
            arrayList.add(0);
            return arrayList;
        }
        long size = max / limit;
        if (max % limit > 0) {
            size += 1;
        }

        System.out.println(size);


        for (int i = 0; i < size; i++) {
            arrayList.add(i * limit);
        }

        return arrayList;
    }

}
